package cn.com.system.controller;
import cn.com.system.pojo.Acc;
import cn.com.system.pojo.Accounts;
import cn.com.system.service.AccService;
import cn.com.system.service.AccountsService;
import cn.com.system.tools.Constants;
import cn.com.system.tools.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("accounts1")


public class AccountsController extends BaseController {
    @Resource
    private AccountsService accountsService;
    @Resource
    private AccService accService;

    @RequestMapping("/accounts1.do")
    public String accounts1(Model model, Accounts accounts,
                            @RequestParam(required=false) String queryname,
                            @RequestParam(required=false) String queryUserRole,
                            @RequestParam(required=false) String pageIndex) {

        System.out.println("这是"+queryname);
        System.out.println("这是"+queryUserRole);
        System.out.println("这是"+pageIndex);
        int queryUserRole2 = 0;
        List<Accounts> accounts1 = null;
        int pageSize = Constants.pageSize;
        int currentPageNo = 1;
        if(queryname == null){
            queryname = "";
        }
        if(queryUserRole != null && !queryUserRole.equals("")){
            queryUserRole2 = Integer.parseInt(queryUserRole);
        }
        if(pageIndex != null){
            try{
                currentPageNo = Integer.parseInt(pageIndex);
            }catch(NumberFormatException e){
                System.out.println("转换异常");
            }
        }
        HashMap<String,Object>map=new HashMap<String,Object>();
        map.put("t_title", queryname);
        map.put("e_name", queryUserRole2);
        int totalCount	= accountsService.getUserCount(map);

        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        if(currentPageNo < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }
        HashMap<String,Object>map1=new HashMap<String,Object>();
        map1.put("t_title", queryname);
        map1.put("e_name", queryUserRole2);
        map1.put("currentPageNo", (currentPageNo-1)*pageSize);
        map1.put("pageSize", pageSize);
        System.out.println(map1);
        accounts1 = accountsService.getUserList(map1);
        System.out.println(accounts1);

        model.addAttribute("accounts1", accounts1);
        List<Acc> roleList = null;
        roleList=accService.getRoleList();

        System.out.println("这是+accLst"+roleList);
        model.addAttribute("accList", roleList);
        model.addAttribute("queryUserName", queryname);
        model.addAttribute("queryUserRole", queryUserRole2);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        return "accounts1";
    }
    @RequestMapping("/delAcc.do")
    public String delAcc(@RequestParam(value="t_id",required = false)
                                     Integer t_id){
        int num=accountsService.deleteAcc(t_id);
        return "redirect:/accounts1/accounts1.do";

    }

    @RequestMapping("/update.do")
    public String addAcc2(Model model,Accounts accounts){
        int num=accountsService.updateAcc(accounts);
        System.out.println(num);
        if (num>0){
            return "redirect:/accounts1/accounts1.do";
        }else {
            return "index";
        }


    }

    @RequestMapping("/add-accounts1.do")
    public String addAcc(){
        return "add-accounts1";
    }

    @RequestMapping("/addAcc.do")
    public String addAcc2(Accounts accounts){
        accountsService.addAcc(accounts);
        return "redirect:/accounts1/accounts1.do";
    }
    @RequestMapping(value="/del.do")
    public String delAll(Integer[] ids){
        System.out.println(Arrays.toString(ids));
        accountsService.delAll(ids);
      return "redirect:../accounts1/accounts1.do";
    }




}
